#include "../include/CBombShotBehaviour.h"


//**************************
//Description : Constructor
//Parameters : Value for m_iX, m_iY,
//              m_iSpeedX, m_iSpeedY,
//              m_iWidth, m_iHeight,
//              the ratio of the collision box
//              the type of collision
//              m_iPowerShot
//Note : None
//**************************
CBombShotBehaviour::CBombShotBehaviour(int _iX, int _iY,
                                            int _iSpeedX , int _iSpeedY,
                                            int _iWidth, int _iHeight,
                                            float _fRatioW, float _fRatioH,
                                            TYPE_COLLISION _iTypeCollision,
                                            int _iPowerShot)
    : CDefaultShotBehaviour(_iX, _iY,
                                _iSpeedX, _iSpeedY,
                                _iWidth, _iHeight,
                                _fRatioW, _fRatioH,
                                _iTypeCollision,
                                _iPowerShot),
    m_bInTheAir(true),
    m_iDistanceToDo(DISTANCE)
{
}

//**************************
//Description : Copy Constructor
//Parameters : A CBombShotBehaviour
//Note : None
//**************************
CBombShotBehaviour::CBombShotBehaviour(CBombShotBehaviour & _BombShot)
    : CDefaultShotBehaviour(_BombShot),
    m_bInTheAir(_BombShot.m_bInTheAir)
{
}

//**************************
//Description : Destructor
//Parameters : None
//Note : None
//**************************
CBombShotBehaviour::~CBombShotBehaviour()
{
}

//**************************
//Description : Test if the entity is in collision with _Entity
//Parameters : None
//Return Value : true if there is collision, otherwise false
//Note : Collision can be different for entity (for example a bomb don't collide all the time, because it's in the air)
//**************************
bool CBombShotBehaviour::CollideWith(IDefaultEntityBehaviour * _Entity) const
{
    if ( !m_bInTheAir )
    {
        if ( _Entity != NULL && m_iTypeCollision == _Entity->GetTypeCollision() )
        {
            // This is our width :
            //  [*********************]
            // With a ratio of 20 %
            //  [--------*****--------]
            //             ^
            //            m_iX
            // The x position of the left side is equal to m_iX - (width / 2)
            // The x position of the right side is equal to m_iX + (width / 2)
            // The x position of the left side with a ratio is equal to m_iX - (width*ratio / 2)
            // The x position of the right side is equal to m_iX + (width*ratio / 2)
            // The ratio width is at
            // Same for the height

            int iHalfCollisionBoxWidth_This = (int)(m_iWidth * m_fCollisionBoxRatioWidth / 2.0f);
            int iHalfCollisionBoxHeight_This = (int)(m_iHeight * m_fCollisionBoxRatioHeight / 2.0f);

            int iHalfCollisionBoxWidth_Entity = (int)(_Entity->GetWidth() * _Entity->GetRatioWidth() / 2.0f);
            int iHalfCollisionBoxHeight_Entity = (int)(_Entity->GetHeight() * _Entity->GetRatioHeight() / 2.0f);

            int TopThis = (int)( m_iY - iHalfCollisionBoxHeight_This );
            int TopEntity = (int)( _Entity->GetY() - iHalfCollisionBoxHeight_Entity );

            int BottomThis = (int)( m_iY + iHalfCollisionBoxHeight_This );
            int BottomEntity = (int)( _Entity->GetY() + iHalfCollisionBoxHeight_Entity );

            int LeftThis = (int)( m_iX - iHalfCollisionBoxWidth_This );
            int LeftEntity = (int)( _Entity->GetX() - iHalfCollisionBoxWidth_Entity );

            int RightThis = (int)( m_iX + iHalfCollisionBoxWidth_This );
            int RightEntity = (int)( _Entity->GetX() + iHalfCollisionBoxWidth_Entity );

            if (BottomThis < TopEntity)
                return false;
            if (TopThis > BottomEntity)
                return false;
            if (RightThis < LeftEntity)
                return false;
            if (LeftThis > RightEntity)
                return false;

            return true;
        }
        else
            return false;
    }
    else
        return false;
}

//**************************
//Description : Return wheter the entity is out of the screen or not
//Parameters : None
//Return Value : True if the entity is out of the screen, otherwise false
//Note : The bomb is out of the screen if it has reach the ground of if it isn't in the screen anymore
//**************************
bool CBombShotBehaviour::IsOutOfScreen() const
{
    return ( !m_bInTheAir ||
        !((m_iX + m_iWidth / 2) >= 0
        && (m_iX - m_iWidth / 2) < WINDOW_WIDTH
        && (m_iY + m_iHeight / 2) >= 0
        && (m_iY - m_iHeight / 2) < WINDOW_HEIGHT) );
}

//**************************
//Description : Update the trajectory of the entity
//Parameters : None
//Return Value : None
//Note : None
//**************************
void CBombShotBehaviour::EntityTrajectory()
{
    m_iDistanceToDo -= abs(m_iSpeedY);
    m_iY += m_iSpeedY;
    m_iX += m_iSpeedX;

    if ( m_iDistanceToDo <= 0 )
        m_bInTheAir = false;
}

